<template>
    <div class="login-container">
        <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            auto-complete="on"
            label-position="left"
        >
            <div class="title-container">
                <h3 class="title">新冠物资管理系统</h3>
            </div>

            <el-form-item prop="username">
                <span class="svg-container">
                    <div class="el-icon-user"></div>
                </span>
                <el-input
                    ref="username"
                    v-model="loginForm.username"
                    placeholder="请输入用户名"
                    name="username"
                    type="text"
                    tabindex="1"
                    auto-complete="on"
                />
            </el-form-item>

            <el-form-item prop="password">
                <span class="svg-container">
                    <!-- <svg-icon icon-class="password" /> -->
                    <div class="el-icon-lock"></div>
                </span>
                <el-input
                    :key="passwordType"
                    ref="password"
                    v-model="loginForm.password"
                    :type="passwordType"
                    placeholder="请输入密码"
                    name="password"
                    tabindex="2"
                    auto-complete="on"
                    @keyup.enter.native="handleLogin"
                />
                <span class="show-pwd" @click="showPwd">
                    <div :class="passwordType === 'password' ? 'el-icon-lock' : 'el-icon-unlock'" />
                </span>
            </el-form-item>

            <el-button
                :loading="loading"
                type="primary"
                style="width: 100%; margin-bottom: 30px"
                @click.native.prevent="handleLogin"
                >登录</el-button
            >

            <div class="tips">
                <span style="margin-right: 20px">username: admin</span>
                <span> password: 123456</span>
            </div>
        </el-form>

        <!-- 验证码 -->
        <Vcode
            :show="isShow"
            :imgs="[Img1, Img2]"
            @success="success"
            @close="close"
            :canvasWidth="500"
            :canvasHeight="350"
        />
    </div>
</template>

<script>
import Vcode from "vue-puzzle-vcode";
import Img1 from "../../assets/img1.jpeg";
import Img2 from "../../assets/img2.jpeg";
export default {
    name: "XgwzVueLogin",

    data() {
        return {
            loginForm: {
                username: "admin",
                password: "123456",
            },
            // 表单验证，需要在 el-form-item 元素中增加 prop 属性
            loginRules: {
                username: [{ required: true, message: "账号不可为空", trigger: "blur" }],
                password: [{ required: true, message: "密码不可为空", trigger: "blur" }],
            },

            loading: false,
            passwordType: "password",
            redirect: undefined,
            isShow: false, // 验证码模态框是否出现
            Img1,
            Img2,
        };
    },
    components: {
        Vcode,
    },
    mounted() {},

    methods: {
        // 显示密码
        showPwd() {
            if (this.passwordType === "password") {
                this.passwordType = "";
            } else {
                this.passwordType = "password";
            }
            this.$nextTick(() => {
                this.$refs.password.focus();
            });
        },
        // 登录
        handleLogin() {
            // 为表单绑定验证功能
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    // 使用 vue-router 路由到指定页面，该方式称之为编程式导航
                    this.isShow = true;
                } else {
                    return false;
                }
            });
        },

        //  获取用户信息
        async getUserInfo() {
            const { data: res } = await this.$http.get("user/info");
            // console.log(res);
            if (res.code == 200) {
                this.userInfo = res.data;
                this.$store.commit("setUserInfo", res.data);
                await this.$router.push("/home");
            } else {
                return this.$message.error("获取用户信息失败");
            }
        },

        // 验证码通过后的回调函数
        async success() {
            this.loading = true;
            // 发送ajax请求
            const { data: res } = await this.$http.post(
                "user/login?username=" + this.loginForm.username + "&password=" + this.loginForm.password
            );
            console.log(res);
            if (res.code == 200) {
                this.$message({
                    title: "登入成功",
                    message: "欢迎你进入系统",
                    type: "success",
                });
                // 保存 token
                localStorage.setItem("JWT_TOKEN", res.data);
                this.getUserInfo();
                this.$router.push("home");
            } else {
                this.isShow = false;
                this.$message.error({
                    title: "登入失败",
                    message: res.msg,
                    type: "error",
                });
            }
        },
        // 关闭验证码窗体
        close() {
            this.isShow = false;
        },
    },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

@supports (-webkit-mask: none) and (not (cater-color: #fff)) {
    .login-container .el-input input {
        color: #fff;
    }
}

/* reset element-ui css */
.login-container {
    .el-input {
        display: inline-block;
        height: 47px;
        width: 85%;

        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            color: #fff;
            height: 47px;
            caret-color: #fff;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px #283443 inset !important;
                -webkit-text-fill-color: #fff !important;
            }
        }
    }

    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
    }
}
</style>

<style lang="scss" scoped>
.login-container {
    min-height: 100%;
    width: 100%;
    background-color: #2d3a4b;
    overflow: hidden;

    .login-form {
        position: relative;
        width: 520px;
        max-width: 100%;
        padding: 160px 35px 0;
        margin: 0 auto;
        overflow: hidden;
    }

    .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
            &:first-of-type {
                margin-right: 16px;
            }
        }
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        color: #889aa4;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
    }

    .title-container {
        position: relative;

        .title {
            font-size: 26px;
            color: #eee;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: #889aa4;
        cursor: pointer;
        user-select: none;
    }
}
</style>
